<template>
  <el-dialog
    :title="layerTitle"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="设备编号" prop="eqmNo">
            <el-input v-model="dataForm.eqmNo" placeholder="设备编号" :disabled="disabled" class="eqmNo"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备名称" prop="eqmName">
            <el-input v-model="dataForm.eqmName" placeholder="设备名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规格型号" prop="eqmSize">
            <el-input v-model="dataForm.eqmSize" placeholder="规格型号"></el-input>
            </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生产厂商" prop="eqmFactory">
            <el-input v-model="dataForm.eqmFactory" placeholder="生产厂商"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="安装地点" prop="eqmAddr">
            <el-input v-model="dataForm.eqmAddr" placeholder="安装地点"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="启用日期" prop="eqmDate">
            <el-date-picker
              clear-icon
              type="date"
              placeholder="选择启用日期"
              v-model="dataForm.eqmDate"
              style="width: 100%;"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="功效" prop="eqmScope">
            <el-input v-model="dataForm.eqmScope" placeholder="功效"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注信息" prop="remarks">
        <el-input type="textarea" v-model="dataForm.remarks" placeholder="备注信息"></el-input>
      </el-form-item>
      <el-form-item label="图片上传" prop="picUpload">
        <el-input v-model="dataForm.picUpload" placeholder="图片上传"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        isAdd: false,
        layerTitle: '', // 弹出框标题
        disabled: false,
        dataForm: {
          eqmNo: '',
          eqmName: '',
          eqmSize: '',
          eqmFactory: '',
          eqmAddr: '',
          eqmDate: '',
          eqmScope: '',
          status: '',
          createBy: '',
          createDate: '',
          updateBy: '',
          updateDate: '',
          remarks: ''
        },
        dataRule: {
          eqmNo: [
            { required: true, message: '设备编号不能为空', trigger: 'blur' }
          ],
          eqmName: [
            { required: true, message: '设备名称不能为空', trigger: 'blur' }
          ],
          eqmSize: [
            { required: true, message: '规格型号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (row) {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (row) {
            this.layerTitle = '编辑'
            this.$http({
              url: this.$http.adornUrl(`${this.$API.BASE_INFO.EQUIPMENT_INFO.INFO}/${row.eqmNo}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              this.dataForm = data.equipmentInfo
              this.isAdd = false
              this.disabled = true
            })
          } else {
            this.layerTitle = '新增'
            this.isAdd = true
            this.disabled = false
            console.log(this.$route)
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`${this.isAdd ? this.$API.BASE_INFO.EQUIPMENT_INFO.SAVE : this.$API.BASE_INFO.EQUIPMENT_INFO.UPDATE}`),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
